package com.atguigu.tingshu.order.api;

import com.atguigu.tingshu.common.login.TsLogin;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.model.order.OrderInfo;
import com.atguigu.tingshu.order.service.OrderInfoService;
import com.atguigu.tingshu.query.order.OrderInfoQuery;
import com.atguigu.tingshu.vo.order.OrderInfoVo;
import com.atguigu.tingshu.vo.order.TradeVo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.aspectj.weaver.ast.Var;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@Tag(name = "订单管理")
@RestController
@RequestMapping("api/order/orderInfo")
@SuppressWarnings({"all"})
public class OrderInfoApiController {

    @Autowired
    private OrderInfoService orderInfoService;


    @TsLogin
    @Operation(summary = "保存订单信息")
    @GetMapping("/findUserPage/{pageNo}/{pageSize}")
    public Result findUserPage(@PathVariable Long pageNo,
                               @PathVariable Long pageSize,
                               @RequestParam(name = "orderStatus", required = false) String orderStatus){
        Long userId = AuthContextHolder.getUserId();
        //  封装到分页Page对象中
        Page<OrderInfo> page = new Page<>(pageNo, pageSize);
        IPage<OrderInfo> iPage   = orderInfoService.findUserPage(page, orderStatus, userId);
        return Result.ok(iPage);
    }

    @Operation(summary = "根据订单id查询订单")
    @GetMapping("/getOrderInfo/{orderNo}")
    public Result getOrderInfo(@PathVariable String orderNo) {
        //	调用服务层方法;
        OrderInfo orderInfo = orderInfoService.getOrderInfo(orderNo);
        //	返回数据
        return Result.ok(orderInfo);
    }

    @TsLogin
    @Operation(summary = "提交订单")
    @PostMapping("/submitOrder")
    public Result submitOrder(@RequestBody OrderInfoVo orderInfoVo) {
        //获取用户id
        Long userId = AuthContextHolder.getUserId();
        //调用服务层方法
        String orderNo = orderInfoService.submitOrder(orderInfoVo, userId);
        Map<String, Object> map = new HashMap<>();
        //把订单编号存储到map 便于后续查询
        map.put("orderNo", orderNo);
        //返回数据
        return Result.ok(map);
    }

    @TsLogin
    @Operation(summary = "获取订单结算页")
    @PostMapping("/trade")
    public Result trade(@RequestBody TradeVo tradeVo) {
        Long userId = AuthContextHolder.getUserId();
        OrderInfoVo orderInfoVo = orderInfoService.trade(tradeVo, userId);
        return Result.ok(orderInfoVo);
    }
}

